extends Node

@export var mob_scene: PackedScene
var score

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass


func game_over() -> void:
	$ScoreTimer.stop()
	$MobTimer.stop()
	$HUD.show_game_over() # Replace with function body.
	$Music.stop()
	$DeathSound.play()

func new_game():
	get_tree().call_group("mobs", "queue_free")
	score = 0
	$Player.start($StartPosition.position)
	$StartTimer.start()
	$HUD.update_score(score)
	$HUD.show_message("Get Ready")
	$Music.play()


func _on_mob_timer_timeout() -> void:
	#创建一个新Mob场景实例

	var mob = mob_scene.instantiate()

	#选择path2d任一位置
	var mob_spawn_location = $MobPath/MobSpawnLocation
	mob_spawn_location.progress_ratio = randf()

	#设置怪物的置到上述的址
	mob.position  = mob_spawn_location.position

	#设置怪物的方向和路径方向垂直
	var direction = mob_spawn_location.rotation + PI/2

	# 方向变量增加些随机值
	direction +=randf_range(-PI /4,PI/4)
	mob.rotation = direction

	#选择物垂直方向
	var velocity = Vector2(randf_range(150.0,250.0),0.0)
	mob.linear_velocity = velocity.rotated(direction)

	#创建怪物并添加到main 场景
	add_child(mob)



func _on_score_timer_timeout() -> void:
	score += 1 # Replace with function body.
	$HUD.update_score(score)


func _on_start_timer_timeout() -> void:
	$MobTimer.start()
	$ScoreTimer.start()
